﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Blog.Core
{
    public class ViewController
    {
        public static string BlogConnectionString
        {
            get
            {
                return Factory.Instance.Repository.BlogConnectionString;
            }
            set
            {
                Factory.Instance.Repository.BlogConnectionString = value;
            }
        }

        public static string ApplicationConnectionString
        {
            get
            {
                return Factory.Instance.Repository.ApplicationConnectionString;
            }
            set
            {
                Factory.Instance.Repository.ApplicationConnectionString = value;
            }
        }

        public static FindResultCollection Find(string text, int pageID, bool findHeaders, bool findContext)
        {
            Core.ThemeCollection col = new Core.ThemeCollection();
            int count = col.Find(pageID, text, findHeaders, findContext);

            return new FindResultCollection(count, (from t in col
                                                    select new FindResultItem(t.ID, t.ThemeHeader)).ToArray());
        }

        public static Core.ThemeCollection GetThemes()
        {
            Core.ThemeCollection col = new Core.ThemeCollection();
            col.Load();
            return col;
        }

        public static Theme GetTheme(int id)
        {
            return new Core.ThemeCollection().GetOne(id);
        }

        public static void DeleteTheme(int id)
        {
            Theme theme = GetTheme(id);
            if (theme != null)
            {
                theme.SetDeleted();
                theme.Save();
            }
        }

        public static void EditTheme(int id, string header, string context)
        {
            Theme theme = GetTheme(id);
            if (theme != null)
            {
                theme.ThemeHeader = header;
                theme.Description = context;
                theme.Save();
            }
        }

        public static IEnumerable<MessageTreeItem> GetMessagesTree(int themeId)
        {
            return GetMessagesTree(GetTheme(themeId));
        }

        public static IEnumerable<MessageTreeItem> GetMessagesTree(Theme theme)
        {
            List<MessageTreeItem> res = new List<MessageTreeItem>();
            if (theme == null)
                return res;

            FillTree(theme.GetMessages(true), null, null, res, 0);
            return res;
        }

        private static void FillTree(MessageCollection collection, Core.Message parentMessage, MessageTreeItem parentTreeItem, List<MessageTreeItem> res, int level)
        {
            foreach (Core.Message mes in collection.GetLevel(
                parentMessage == null ? 0 : parentMessage.MessageTreeId,
                parentMessage == null ? 0 : parentMessage.ID))
            {
                MessageTreeItem item = new MessageTreeItem(mes.ID, mes.MessageTreeId, level, mes.UserName, mes.ModifyDateTime, mes.MessageText);
                res.Add(item);

                if (parentTreeItem != null)
                    parentTreeItem.HasChldren = true;

                FillTree(collection, mes, item, res, level + 1);
            }
        }
    }

    public class MessageTreeItem
    {
        public MessageTreeItem(int id, int treeId, int level, string user, DateTime? dt, string text)
        {
            ID = id;
            TreeID = treeId;
            Level = level;
            User = user;
            Date = dt;
            Text = text;
            HasChldren = false;
        }

        public int ID { get; private set; }
        public int TreeID { get; private set; }
        public int Level { get; private set; }
        public string User { get; private set; }
        public DateTime? Date { get; private set; }
        public string Text { get; private set; }
        public bool HasChldren { get; internal set; }
    }

    public class FindResultItem
    {
        public FindResultItem()
        { 
        }
        public FindResultItem(int id, string header)
        {
            ThemeId = id;
            ThemeHeader = header;
        }
        public int ThemeId { get; set; }
        public string ThemeHeader { get; set; }
    }

    public class FindResultCollection
    {
        public FindResultCollection()
        { 
        }
        public FindResultCollection(int pagesCount, FindResultItem[] items)
        {
            PagesCount = pagesCount;
            Items = items;
        }
        public int PagesCount { get; set; }
        public FindResultItem[] Items { get; set; }
    }
}
